package com.example.controller;

import com.example.config.CustomConfig;
import com.example.dao.SlowSqlInfo;
import com.example.dao.SlowSqlInfoRepository;
import com.example.service.SlowSqlInfoService;
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
public class SlowSqlInfoController {
    private final SlowSqlInfoService slowSqlInfoService;
    private final SlowSqlInfoRepository slowSqlInfoRepository;
    private final CustomConfig customConfig;

    public SlowSqlInfoController(SlowSqlInfoService slowSqlInfoService, SlowSqlInfoRepository slowSqlInfoRepository, CustomConfig customConfig) {
        this.slowSqlInfoService = slowSqlInfoService;
        this.slowSqlInfoRepository = slowSqlInfoRepository;
        this.customConfig = customConfig;
    }

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }

    @GetMapping(value = "/list")
    @ResponseBody
    public String list(@RequestParam(defaultValue = "false") Boolean recent,
                       @RequestParam(defaultValue = "false") Boolean jsonFormat) {
        return slowSqlInfoService.getSlowSqlInfo(recent, jsonFormat);
    }

    @GetMapping("/slowSql")
    public String slowSql(
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) String fingerprint,
            @RequestParam(required = false) String db,
            @RequestParam(required = false) String host,
            Model model) {

        // 构建动态查询
        Specification<SlowSqlInfo> spec = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (id != null) {
                predicates.add(cb.equal(root.get("id"), id));
            }
            if (StringUtils.hasText(fingerprint)) {
                predicates.add(cb.like(root.get("fingerprint"), "%" + fingerprint + "%"));
            }
            if (StringUtils.hasText(db)) {
                predicates.add(cb.equal(root.get("db"), db));
            }
            if (StringUtils.hasText(host)) {
                predicates.add(cb.equal(root.get("host"), host));
            }

            return cb.and(predicates.toArray(new Predicate[0]));
        };

        model.addAttribute("slowSqlList", slowSqlInfoRepository.findAll(spec));
        return "slowSql";
    }

    @GetMapping(value = "/test")
    @ResponseBody
    public String test() {
        System.out.println(customConfig.getQueryCountThreshold());
//        System.out.println(JSON.toJSONString(customConfig));
        return "test";
    }

}
